<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="clienteteRules" :model="clienteteForm" label-position="left" label-width="140px">

      <el-form-item label="姓名" prop="fullname">
        <el-input v-model="clienteteForm.fullname" />
      </el-form-item>
      <el-form-item prop="avatar" label="头像" style="margin-bottom: 30px;">
        <Upload v-model="clienteteForm.avatar" />
      </el-form-item>
      <el-form-item label="性别">
        <el-input v-model="clienteteForm.sex" />
      </el-form-item>
      <el-form-item label="生日">
        <el-date-picker 
          v-model="clienteteForm.birthday"
          format="yyyy-mm-dd"
          value-format="yyyy-mm-dd"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="clienteteForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="clienteteForm.status.value === 5" type="danger" @click="handleDelete(clienteteForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveCientete, removeCienteteById, getCientete } from '@/api/customer/clientete'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/SingleImage' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      clienteteForm: {
        fullname: '',
        avatar: '',
        sex: '',
        birthday: '',
        remark: '',
        status: { label: '', value: 1 }
      },
      clienteteRules: []
    }
  },
  created() {
    let clienteteId = this.$route.query && this.$route.query.clienteteId
    if (clienteteId !== undefined) {
      clienteteId = parseInt(clienteteId)
      if (!isNaN(clienteteId)) {
        this.listLoading = true
        const formData = {
          'clienteteId': clienteteId
        }
        getCientete(formData).then(response => {
          if (response.status === 200) {
            this.clienteteForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(clienteteId)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'clientete' })
    },
    createData() {
      this.listLoading = true

      const _clienteteForm = this.clienteteForm
      _clienteteForm.status = _clienteteForm.status.value
      if (_clienteteForm.identity > 0) {
        _clienteteForm.clienteteId = _clienteteForm.identity
      }
      saveCientete(_clienteteForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'clientete' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      this.listLoading = true
      const formData = {
        'cienteteId': row.identity
      }

      removeCienteteById(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'clientete' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
